package net.dgg.zz.credential.warningConfigure.controller;

import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DataTableResponse;
import net.dgg.zz.credential.certificateType.service.CertificateTypeService;
import net.dgg.zz.credential.storageCapacity.entity.StorageCapacityVo;
import net.dgg.zz.credential.storageCapacity.service.StorageCapacityService;
import net.dgg.zz.credential.warningConfigure.entity.WarningConfigure;
import net.dgg.zz.credential.warningConfigure.service.SystemConfigureService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/zs/warning/aptitude")
public class AptitudeWarningConfigureController extends DggBaseController {

    @Autowired
    private SessionManager sessionManager;

    @Autowired
    private OrganizationManager organizationManager;

    @Autowired
    StorageCapacityService storageCapacityService;

    @Autowired
    private SystemConfigureService systemConfigureService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private CertificateTypeService certificateTypeService;

    /**
     * 显示资质库容预警页面
     *
     * @param model
     * @param params
     * @return
     */
    @RequestMapping("/storage_index")
    public String aptitudeIndex(Model model, Map params) {
        //资质等级
        List<Map> certificateLevels = commonService.queryTreeBook(CredentialConstant.QUALIFICATION_LEVEL, 1, 2, null);
        model.addAttribute("certificateLevels", certificateLevels);
        // 适用范围
        List<Map> applicationScopes = commonService.queryTreeBook(CredentialConstant.CREDENTIAL_APPLICATION_SCOPE, 1, 2, null);
        model.addAttribute("applicationScopes", applicationScopes);
        model.addAttribute("generalType", CredentialConstant.ZZ_CREDENTIAL);
        return "zzsc/credential/warningConfigure/storage_index";
    }

    /**
     * 普通证书库容预警
     *
     * @param model
     * @return
     */
    @NLog()
    @RequestMapping("/warning_data")
    @ResponseBody
    public DataTableResponse warningData(@RequestParam Map params, HttpServletRequest request) {
        try {
            DataTableRequest r = DataTableUtils.getParam(request);
            params.put("page", r.getPage());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
            List<StorageCapacityVo> storageCapacities = storageCapacityService.queryStorageCapacityWithPage(params);
            return new DataTableResponse().data(params, storageCapacities);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 显示资质过期预警页面
     *
     * @param
     * @return
     */
    @RequestMapping("/stale_index")
    public String staleIndex(@RequestParam Map params, Model model) {
        Iterator<Map.Entry<String, String>> map = params.entrySet().iterator();
        while (map.hasNext()) {
            Map.Entry<String, String> entry = map.next();
            model.addAttribute(entry.getKey(), entry.getValue());
        }
        params.put("generalType", CredentialConstant.ZZ_CREDENTIAL);
        params.put("parentId", 0);
        //资质类型
        List<Map> typeList = certificateTypeService.getCertificateType(params);
        model.addAttribute("typeList", typeList);
        //查询过期预警的天数
        UserEntity user = sessionManager.getCurrentUser();
        Long orgid = organizationManager.findOrgParentIdByIdAndOrgLevel(user.getOrgId(), "ORG_LEVEL_1");
        if (orgid == null) {
            orgid = user.getOrgId();
        }
        params.put("signOrgId", orgid);
        int[] types = {1,2,4, 5, 7};
        params.put("types", types);
        findZiDian(model, params);
        model.addAttribute("signOrgId", orgid);
        return "zzsc/credential/warningConfigure/stale_index";
    }

    /**
     * 查询字典的资质类型、超期状态、适用范围、资质等级
     *
     * @param model
     * @param params types:数组1:资质类型、2.超期状态、3.资质状态、4.适用范围、5.资质等级6.查询预警值7.过期预警值
     */
    public void findZiDian(Model model, Map params) {
        int[] types = (int[]) params.get("types");
        for (int type : types) {
            if (type == 1) {
                //资质类型
                params.put("generalType", CredentialConstant.ZZ_CREDENTIAL);
                params.put("parentId", 0);
                List<Map> typeList = certificateTypeService.getCertificateType(params);
                model.addAttribute("typeList", typeList);
            } else if (type == 2) {
                //超期状态
                List<Map> overdueStatuss = commonService.queryTreeBook(CredentialConstant.CERTIFICATE_APTITUDE_EXCEED, 1, 2, null);
                model.addAttribute("overdueStatuss", overdueStatuss);
            } else if (type == 3) {
                //资质状态
                List<Map> certificateStatus = commonService.queryTreeBook(CredentialConstant.CERTIFICATE_APTITUDE_STATUS, 1, 2, null);
                model.addAttribute("certificateStatus", certificateStatus);
            } else if (type == 4) {
                //适用范围
                List<Map> applicationScopes = commonService.queryTreeBook(CredentialConstant.CREDENTIAL_APPLICATION_SCOPE, 1, 2, null);
                model.addAttribute("applicationScopes", applicationScopes);
            } else if (type == 5) {
                //资质等级
                List<Map> certificateLevels = commonService.queryTreeBook(CredentialConstant.QUALIFICATION_LEVEL, 1, 2, null);
                model.addAttribute("certificateLevels", certificateLevels);
            } else if (type == 6) {
                params.put("warningType", CredentialConstant.ZZ_CREDENTIAL);
                params.put("warningName", "STORAGE_WARNING");
                Map<String, Object> configureMap = systemConfigureService.querySingleWarning(params);
                model.addAttribute("configValue", configureMap.get("configValue"));
            } else if (type == 7) {
                params.put("warningType", CredentialConstant.ZZ_CREDENTIAL);
                params.put("warningName", "CERTIFICATE_EXPIRE_WARNING");
                Map<String, Object> configureMap = systemConfigureService.querySingleWarning(params);
                model.addAttribute("expireWarning", configureMap.get("configValue") == null ? 0 : configureMap.get("configValue"));
            }
        }
    }

    /**
     * 资金池设置
     *
     * @param model
     * @return
     */
    @NLog
    @RequestMapping("/set_captial_pool_warning_page")
    public String setCaptialPoolWarningPage(Model model) {
        try {
            WarningConfigure warningConfigure = systemConfigureService.findConfigureByOrgId(null, CredentialConstant.GENERAL_CREDENTIAL);
            model.addAttribute("warningConfigure", warningConfigure);
            model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
            return "/zzsc/credential/generalCredential/set_capital_pool_warning";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("noSyb", "您所在的组织机构没有所在事业部，不能进行设置");
            return "/zzsc/credential/generalCredential/set_capital_pool_warning";
        }
    }

}
